<?php

	/**
	 * @package Yomm
	 * @author bsuter
	 *
	 */
	Abstract Class Yomm_Module_Cms Extends Yomm_Controller
	{
		private $page_rewrite = null;
		
		private $nav = null;
			
		public $cat_id = null;
		
		Public Function __construct()
		{
			// call the Framework constructor
			parent::__construct();
		
			$this->view->addJs(SiteConfig::Domain . 'share/js/scriptaculous-min.js');

			$this->nav = $this->buildNavigation();
			
			if(isset($this->get->cat))
			{
				$this->cat_id = $this->db->select("cms_cat", "id", "WHERE rewrite_link='{$this->get->cat}'", false)->id;
			} else {
				$this->cat_id = SiteConfig::DefaultCat;
			}
			
			if(empty($this->get->page) || !isset($this->get->page))
			{
				$obj = $this->get_obj();
				
				if(count($obj) == 0)
				{
					echo '<h1>Startup Error</h1><p>Create a first page and deposit the following as default Page of your category (Standard category is "Default")</p>
						<ol>
							<li>Login  <a href="'.SiteConfig::Domain.'?controller=admin">Backend</a> anmelden</li>
							<li>Login with your credentials u have set while installing the system.</li>
							<li>Create a new page (Pages -> Add)</li>
							<li>Edit category (Pages -> categories) select the desired front page out of the Dropdown and save it.</li>
							<li>Switch pages Online (Pages -> Manage -> Click on the symbol with the red circle)</li>
							<li>Now you can try again by refreshing this page!</li>
						</ol>';
					exit;
				}
				
				$this->page_rewrite = $obj->rewrite_link;
			} else {
				$this->page_rewrite = $this->get->page;
			}
			
			/*
			 * check if there is really only one page for this rewrite name.
			 */
			$cms_block_id = $this->db->select("cms_block", "id", "WHERE rewrite_link='{$this->page_rewrite}'", false);
			
			if(count($cms_block_id) !== 1)
			{
				if(count($cms_block_id) > 1)
				{
					$error = new Yomm_Error("there is a problem with display the content for 0 or more then 1 pages. dublicated or none cms_block content for rewrite '{$this->page_rewrite}'; count value is '{$this->db->sql_numrows}'; runing sql query: '{$this->db->last_query}'");
				}
				$this->redirect();
			}
			
			// check if this page really exists
			if($this->db->select("cms_block",array("COUNT(*)" => "count"), "LEFT JOIN (cms_nav) ON (cms_nav.id = cms_block.nav_id) WHERE cms_block.rewrite_link='{$this->page_rewrite}' AND cms_nav.status='1' AND cms_nav.del='0'", false)->count == 0)
			{
				$this->redirect();
			}
			
			//$parser = new ParseObject($this->page_rewrite, $this->theme, $this->view);
			$parser = new Yomm_Library_CmsPageParser($this->page_rewrite, $this->theme, $this->view);
			
			$cms_obj = $parser->export();
			
			$this->getAndAssignEnv($cms_block_id->id);
			
			$this->view->assign(array(
				'this_cat' => $this->db->select("cms_cat", "rewrite_link", "WHERE id='{$this->cat_id}'", false)->rewrite_link,
				'nav' => $this->nav,
				'this_page' => $this->page_rewrite,
				'this_page_id' => $this->db->select("cms_block", "id", "WHERE rewrite_link='{$this->page_rewrite}'", false)->id,
				'this_url' => Yomm_Library_CmsTools::BuildLinkByRewrite($this->page_rewrite),
				'cms_obj' => $cms_obj,
				'title' => $cms_obj->title,
				'content' => $cms_obj->text
			));
		}
		
		private function getAndAssignEnv($block_id)
		{
			//$q = $this->db->select("cms_env_values", "*", "LEFT JOIN(cms_env) ON (cms_env_values.cms_env_id=cms_env.id) WHERE cms_block_id='{$block_id}'", true);
			$env = array();
			
			foreach($this->db->select("cms_env", "*", false, true) as $item)
			{
				$blockv = $this->db->select("cms_env_values", "value", "WHERE cms_block_id='{$block_id	}' AND cms_env_id='{$item->id}'", false);
				
				if(count($blockv) == 0)
				{
					$env[$item->var_name] = $item->default_value;
				} else {
					$env[$item->var_name] = $blockv->value;
				}
			}
			$this->view->notAppend($env);
		}
		
		private function get_obj()
		{
			return $this->db->select("cms_cat", "cms_block.rewrite_link", "LEFT JOIN (cms_block) ON (cms_block.nav_id=cms_cat.nav_default) WHERE cms_block.language_id='{$this->i18n->id}' AND cms_cat.id='{$this->cat_id}'", false);	
		}
		
	}